import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot, UrlTree} from '@angular/router';
import {MainComponent} from './main.component';
import {Observable} from 'rxjs';
import {MessageService} from '../shared/services/message-service';

@Injectable()
export class CanDeactivateMain implements CanDeactivate<MainComponent> {
  constructor() {
  }

  canDeactivate(
    component: MainComponent,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState: RouterStateSnapshot
  ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    if (!component.canDeactivate){
      MessageService.showGlobalMessage('warning', '请完成测试或终止测试！');
    }
    return component.canDeactivate;
  }
}
